﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import flash.display.*;
    import org.aswing.geom.*;
    import org.aswing.event.*;
    import org.aswing.util.*;

    public class JWindow extends JPopup {

        private var contentPane:Container;
        private var actived:Boolean;
        private var focusWhenDeactive:Component;
        private var lootActiveFrom:JWindow;
        private var focusObject:InteractiveObject;
        private var activable:Boolean;

        public function JWindow(_arg1=null, _arg2:Boolean=false){
            super(_arg1, _arg2);
            setName("JWindow");
            this.actived = false;
            this.focusObject = new Sprite();
            this.focusObject.name = "hidden_focus_obj";
            this.focusObject.visible = false;
            this.activable = true;
            addChild(this.focusObject);
            layout = new WindowLayout();
            setFocusTraversalPolicy(new WindowOrderFocusTraversalPolicy());
            addEventListener(MouseEvent.MOUSE_DOWN, this.__activeWhenPress, true);
            addEventListener(MouseEvent.MOUSE_DOWN, this.__activeWhenPressWindowSelf);
        }
        public static function getWindows(_arg1:Stage=null):Array{
            var _local6:*;
            if (_arg1 == null){
                _arg1 = AsWingManager.getStage();
            };
            var _local2:FocusManager = FocusManager.getManager(_arg1);
            var _local3:ArrayList = _local2.getPopupsVector();
            var _local4:Array = new Array();
            var _local5:int;
            while (_local5 < _local3.size()) {
                _local6 = _local3.get(_local5);
                if ((_local6 is JWindow)){
                    _local4.push(_local6);
                };
                _local5++;
            };
            return (_local4);
        }
        public static function getOwnedWindowsWithOwner(_arg1:DisplayObjectContainer):Array{
            var _local3:Array;
            var _local4:ArrayList;
            var _local5:int;
            var _local6:int;
            var _local7:JPopup;
            var _local2:FocusManager = FocusManager.getManager(_arg1.stage);
            if (_local2){
                _local3 = new Array();
                _local4 = _local2.getPopupsVector();
                _local5 = _local4.size();
                _local6 = 0;
                while (_local6 < _local5) {
                    _local7 = _local4.get(_local6);
                    if ((((_local7 is JWindow)) && ((_local7.getOwner() === _arg1)))){
                        _local3.push(_local7);
                    };
                    _local6++;
                };
                return (_local3);
            };
            return ([]);
        }

        override public function setLayout(_arg1:LayoutManager):void{
            var _local2:WindowLayout;
            if ((_arg1 is WindowLayout)){
                _local2 = (this.layout as WindowLayout);
                super.setLayout(_arg1);
                if (_local2 != null){
                    if (_local2.getTitleBar() != null){
                        _arg1.addLayoutComponent(_local2.getTitleBar(), WindowLayout.TITLE);
                    };
                    if (_local2.getContentPane() != null){
                        _arg1.addLayoutComponent(_local2.getContentPane(), WindowLayout.CONTENT);
                    };
                };
            } else {
                throw (new ArgumentError("Can not set a non-WindowLayout Layout to JWindow"));
            };
        }
        override public function paintImmediately():void{
            var _local1:IntDimension;
            var _local2:IntDimension;
            if (isVisible()){
                _local1 = getMinimumSize();
                _local2 = new IntDimension(Math.max(getWidth(), _local1.width), Math.max(getHeight(), _local1.height));
                setSize(_local2);
                super.paintImmediately();
                revalidateIfNecessary();
            } else {
                super.paintImmediately();
            };
        }
        public function getContentPane():Container{
            var _local1:Container;
            if (this.contentPane == null){
                _local1 = new Container();
                _local1.setLayout(new BorderLayout());
                this.setContentPaneImp(_local1);
            };
            return (this.contentPane);
        }
        public function setContentPane(_arg1:Container):void{
            if (_arg1 != this.contentPane){
                if (_arg1 == null){
                    throw (new Error((this + " Can not set null to be JWindow's contentPane!")));
                };
                this.setContentPaneImp(_arg1);
            };
        }
        private function setContentPaneImp(_arg1:Container):void{
            if (this.contentPane != null){
                this.contentPane.removeFromContainer();
            };
            this.contentPane = _arg1;
            append(this.contentPane, WindowLayout.CONTENT);
        }
        public function getWindowOwner():JWindow{
            return ((owner as JWindow));
        }
        public function getOwnedWindows():Array{
            return (getOwnedWindowsWithOwner(this));
        }
        override public function setVisible(_arg1:Boolean):void{
            super.setVisible(_arg1);
            if (((_arg1) && (this.isActivable()))){
                this.setActive(true);
            } else {
                this.lostActiveAction(stage);
            };
        }
        public function setActivable(_arg1:Boolean):void{
            this.activable = _arg1;
        }
        public function isActivable():Boolean{
            return (this.activable);
        }
        override protected function disposeProcess(_arg1:Stage):void{
            this.lostActiveAction(_arg1);
        }
        public function isActive():Boolean{
            return (this.actived);
        }
        public function setActive(_arg1:Boolean):void{
            if (this.actived != _arg1){
                if (_arg1){
                    this.active();
                } else {
                    this.deactive(stage);
                };
            };
        }
        public function focusAtThisWindow():void{
            var _local1:Component = this.focusWhenDeactive;
            if ((((_local1 == null)) || (!(((((((AsWingUtils.isAncestor(this, _local1)) && (_local1.isShowing()))) && (_local1.isFocusable()))) && (_local1.isEnabled())))))){
                _local1 = getFocusTraversalPolicy().getDefaultComponent(this);
            };
            if (_local1 == null){
                _local1 = this;
            };
            this.focusWhenDeactive = null;
            _local1.requestFocus();
        }
        private function lostActiveAction(_arg1:Stage):void{
            if (this.isActive()){
                this.deactive(_arg1);
                if (((!((this.getLootActiveFrom() == null))) && (this.getLootActiveFrom().isShowing()))){
                    this.getLootActiveFrom().active();
                };
            };
            this.setLootActiveFrom(null);
        }
        private function getLootActiveFrom():JWindow{
            return (this.lootActiveFrom);
        }
        private function setLootActiveFrom(_arg1:JWindow):void{
            if (this.lootActiveFrom != null){
                this.lootActiveFrom.removeEventListener(PopupEvent.POPUP_CLOSED, this.__lootActiveFromHide);
            };
            var _local2:JWindow = this.lootActiveFrom;
            this.lootActiveFrom = _arg1;
            if (this.lootActiveFrom != null){
                this.lootActiveFrom.addEventListener(PopupEvent.POPUP_CLOSED, this.__lootActiveFromHide, false, 0, true);
            };
            if (((!((_arg1 == null))) && ((_arg1.getLootActiveFrom() == this)))){
                _arg1.setLootActiveFrom(_local2);
            };
        }
        private function __lootActiveFromHide(_arg1:PopupEvent):void{
            if (this.lootActiveFrom != null){
                this.setLootActiveFrom(this.lootActiveFrom.lootActiveFrom);
            };
        }
        private function active(_arg1:Boolean=true):void{
            var _local5:JWindow;
            var _local2:FocusManager = FocusManager.getManager(stage);
            if (_local2 == null){
                return;
            };
            this.actived = true;
            var _local3:ArrayList = _local2.getPopupsVector();
            var _local4:int;
            while (_local4 < _local3.size()) {
                _local5 = (_local3.get(_local4) as JWindow);
                if (((!((_local5 == null))) && (!((_local5 == this))))){
                    if (_local5.isActive()){
                        _local5.deactive(_local5.stage, _arg1);
                        if (_local5.isShowing()){
                            this.setLootActiveFrom(_local5);
                        };
                    };
                };
                _local4++;
            };
            _local2.setActiveWindow(this);
            this.focusAtThisWindow();
            dispatchEvent(new WindowEvent(WindowEvent.WINDOW_ACTIVATED, _arg1));
        }
        private function deactive(_arg1:Stage, _arg2:Boolean=true):void{
            this.actived = false;
            var _local3:FocusManager = FocusManager.getManager(_arg1);
            if (_local3 == null){
                return;
            };
            this.focusWhenDeactive = _local3.getFocusOwner();
            if (!AsWingUtils.isAncestor(this, this.focusWhenDeactive)){
                this.focusWhenDeactive = null;
            };
            _local3.setActiveWindow(null);
            dispatchEvent(new WindowEvent(WindowEvent.WINDOW_DEACTIVATED, _arg2));
        }
        override public function getInternalFocusObject():InteractiveObject{
            return (this.focusObject);
        }
        private function __activeWhenPress(_arg1:MouseEvent):void{
            if (this.getWindowOwner() != null){
                this.getWindowOwner().toFront();
            };
            toFront();
            if (((this.isActivable()) && (!(this.isActive())))){
                this.active(false);
            };
        }
        private function __activeWhenPressWindowSelf(_arg1:MouseEvent):void{
            if (_arg1.target == this){
                this.__activeWhenPress(_arg1);
            };
        }

    }
}//package org.aswing 
